﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using MockCommon;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation
{
    public partial class ViewTimesheets : Page
    {
        private ContractorBusiness _contractorBusiness;
        private TimesheetStatusBusiness _timeSheetstatusBusiness;
        private TimesheetBusiness _timeSheetBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private DropDownList _statusDropDownList;
        private ListView _timesheetListView;
        private CheckBox _allSearchCheckBox;
        private TextBox _fromDateTextBox;
        private TextBox _toDateTextBox;
        private Button _filterButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _contractorBusiness = ContractorBusiness.RetrieveInstance();
                _timeSheetstatusBusiness = TimesheetStatusBusiness.RetrieveInstance();
                _timeSheetBusiness = TimesheetBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _statusDropDownList = (DropDownList)DetailContractorUserControl.FindControl("StatusDropDownList");
                _timesheetListView = (ListView)DetailContractorUserControl.FindControl("TimesheetListView");
                _allSearchCheckBox = (CheckBox)DetailContractorUserControl.FindControl("AllSearchCheckBox");
                _fromDateTextBox = (TextBox)DetailContractorUserControl.FindControl("FromDateTextBox");
                _toDateTextBox = (TextBox)DetailContractorUserControl.FindControl("ToDateTextBox");
                _filterButton = (Button)DetailContractorUserControl.FindControl("FilterButton");

                _filterButton.Click += new EventHandler(FilterButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = (User)Session["user"];
                if (user.Role.RoleName == "Consultant")
                {
                    if (!IsPostBack)
                    {
                        if (Request.QueryString["cid"] != null)
                        {
                            LoadTimesheet();
                        }
                        else
                        {
                            Server.Transfer("NotFound.aspx");
                        }
                    }
                }
                else
                {
                    Server.Transfer("NotAuthorized.aspx");
                }
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            try
            {
                string statusname = _statusDropDownList.Text;
                Guid statusId = Guid.Parse(_timeSheetstatusBusiness.GetStatusIdByStatusName(statusname).ToString());
                if (String.IsNullOrEmpty(_fromDateTextBox.Text) || String.IsNullOrEmpty(_toDateTextBox.Text))
                {
                    LoadTimesheet();
                }
                else if (statusId != null)
                {
                    DateTime fromDate = DateTime.Parse(_fromDateTextBox.Text, CultureInfo.CurrentCulture);
                    DateTime toDate = DateTime.Parse(_toDateTextBox.Text, CultureInfo.CurrentCulture);
                    Contractor contractor = _contractorBusiness.GetById(Request.QueryString["cid"]);
                    User user = contractor.User;
                    _timesheetListView.DataSource = _timeSheetBusiness.GetByFilterRange(user.UserName.ToString(), statusId, fromDate, toDate);
                    _timesheetListView.DataBind();
                }
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void LoadTimesheet()
        {
            try
            {
                Contractor contractor = _contractorBusiness.GetById(Request.QueryString["cid"]);
                User user = contractor.User;
                _statusDropDownList.DataSource = _timeSheetstatusBusiness.RetrieveAllTimesheetStatus();
                _statusDropDownList.DataMember = "StatusId";
                _statusDropDownList.DataTextField = "StatusName";
                _statusDropDownList.DataBind();
                _timesheetListView.DataSource = _timeSheetBusiness.GetByContractorUserName(user.UserName);
                _timesheetListView.DataBind();
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void StatusDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string statusname = _statusDropDownList.Text;
                Guid statusId = Guid.Parse(_timeSheetstatusBusiness.GetStatusIdByStatusName(statusname).ToString());
                if (statusId != null)
                {
                    Contractor contractor = _contractorBusiness.GetById(Request.QueryString["cid"]);
                    User user = contractor.User;
                    _timesheetListView.DataSource = _timeSheetBusiness.GetByFilter(user.UserName, statusId);
                    _timesheetListView.DataBind();
                }
            }
            catch (MockException exception)
            {

                _errorLabel.Text = exception.ExceptionMessage;
            }

        }

        protected void AllSearchCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (_allSearchCheckBox.Checked == true)
            {
                _statusDropDownList.AutoPostBack = false;
            }
            else
            {
                _statusDropDownList.AutoPostBack = true;
            }
        }
    }
}